/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Event;

/**
 *
 * @author Varadha
 */
public class EventDAO extends BaseEntityDAO
{

    public EventDAO()
    {
        // initialization
    }

    /**
     * Insert event into database
     * @param event the new event entity to be inserted
     * @return newly created event number or a 0 on error
     */
    public int insertEvent(Event event)
    {
        return save(event);
    }

    /**
     * Delete event that matches the supplied criteria
     * @param event the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteEvent(Event event)
    {
        return delete(event);
    }

    /**
     * Update the event using data from the event Data Object
     * @param event the event Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateEvent(Event event)
    {
        if (event.getEventID() == 0)
        {
            return false;
        }

        if (update(event))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the event that matches the supplied criteria
     * @param event the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Event findEvent(Event event)
    {
        String sql = null;

        if (event.getEventID() != 0)
        {
            sql = buildQuerySQL(event);
        }
        else
        {
            return null;
        }

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Event) this.getObject(crs, event);
    }

    /**
     * Get all events that match the supplied criteria
     * @param event supplied criteria
     * @return a RowSet
     */
    public RowSet getEventsRS(Event event)
    {
        String sql = buildQuerySQL(event);
        return query(sql);
    }

    /**
     * Get all events that match the supplied criteria
     * @param event supplied criteria
     * @return a collection of Event Data Objects
     */
    public Collection<Event> getEventsDO(Event event)
    {
        String sql = buildQuerySQL(event);

        RowSet crs = query(sql);
        Collection<Event> events = new ArrayList<Event>();
        try
        {
            while (crs.next())
            {
                Event eventObject = (Event) this.getObject(crs, event);
                Event eventDO = new Event();
                eventDO.setEventID(eventObject.getEventID());
                eventDO.setVenueID(eventObject.getVenueID());
                eventDO.setName(eventObject.getName());
                eventDO.setDescription(eventObject.getDescription());
                eventDO.setBeginDateTime(eventObject.getBeginDateTime());
                eventDO.setEndDateTime(eventObject.getEndDateTime());
                eventDO.setPrice(eventObject.getPrice());
                eventDO.setKeywords(eventObject.getKeywords());
                eventDO.setType(eventObject.getType());
                eventDO.setCancelled(eventObject.isCancelled());
                events.add(eventDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return events;
    }

    /**
     * Get all events that match the supplied criteria
     * @param event supplied criteria
     * @return a collection of Event Data Objects
     */
    public Collection<Event> getEventsDOByKeyword(Event event)
    {
        String sql = "select * from event where name like \'%" + event.getName() + "%\' and beginDateTime > '" + event.getBeginDateTime().toString() + "' and endDateTime < '" + event.getEndDateTime().toString() + "'";

        RowSet crs = query(sql);
        Collection<Event> events = new ArrayList<Event>();
        try
        {
            while (crs.next())
            {
                Event eventObject = (Event) this.getObject(crs, event);
                Event eventDO = new Event();
                eventDO.setEventID(eventObject.getEventID());
                eventDO.setVenueID(eventObject.getVenueID());
                eventDO.setName(eventObject.getName());
                eventDO.setDescription(eventObject.getDescription());
                eventDO.setBeginDateTime(eventObject.getBeginDateTime());
                eventDO.setEndDateTime(eventObject.getEndDateTime());
                events.add(eventDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return events;
    }
}
